package demo.thread;

import java.util.List;
import java.util.concurrent.*;

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 8, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.DiscardPolicy());
        MyRunnable runnable1 = new MyRunnable("第一个任务");
        MyRunnable runnable2 = new MyRunnable("第二个任务");
        MyRunnable runnable3 = new MyRunnable("第三个任务");

        executor.execute(runnable1);
        executor.execute(runnable2);
        executor.execute(runnable3);

        Thread.sleep(3000);
        System.out.println("所有任务执行完毕");
    }
}

class MyRunnable implements Runnable{
    private String name;
    public MyRunnable(String name){
        this.name = name;
    }
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " 正在执行 " + this.name + " 时间 " + System.currentTimeMillis());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
